"use strict";
var jmpmode = 0;
function main() {
    var oauthWin;
    console.log("hi there.");
    var sp1 = document.createElement("span");
    sp1.textContent = "hello world";
    document.body.appendChild(sp1);
    window.addEventListener("message", function (e) {
        if (e.data["cmd"] == undefined)
            return;
        var cmd = e.data["cmd"];
        if (cmd != "backcall")
            return;
        var url = e.data["url"];
        //if(e["cmd"]=="backcall")
        var codeindex = url.indexOf("code=");
        var token = url.substr(codeindex + 5);
        console.log("recive token=:" + token);
        var span = document.createElement("span");
        span.textContent = "token=" + token;
        document.body.appendChild(span);
        if (jmpmode == 1) {
            var tourl = window.location.href;
            var i = tourl.indexOf("?");
            if (i > 0)
                tourl = tourl.substring(0, i);
            oauthWin === null || oauthWin === void 0 ? void 0 : oauthWin.postMessage({ "cmd": "jmpto", "url": tourl + "?token=" + token }, "*");
            window.close();
        }
        else if (jmpmode == 2) {
            oauthWin === null || oauthWin === void 0 ? void 0 : oauthWin.postMessage({ "cmd": "close" }, "*");
        }
    });
    var ori = window.origin;
    var frame = document.createElement("iframe");
    frame.style.width = "500px";
    frame.style.height = "300px";
    frame.name = "f1";
    //frame
    frame.onload = function () {
        console.log("frame=" + frame.src);
        // window.parent.postMessage("hello",ori);
        // frame.baseURI;
        // //let href= frame.contentWindow?.location.href;
        // //
    };
    frame.onloadeddata = function () {
        console.log("onloadeddata frame=" + frame.src);
    };
    document.body.appendChild(frame);
    var clientid = "05ce1cd04ef08dde351cd868a26b6f5ce2b7b9471f18c299e4b511d74e59fc54";
    var redirect_uri = "http://lightsever.gitee.io/fakenes/jmpback.html";
    var src = "https://gitee.com/oauth/authorize?" +
        "client_id=" + clientid +
        "&redirect_uri=" + encodeURIComponent(redirect_uri) + "?aasdfsdf" +
        "&response_type=code" +
        "&scope=" + encodeURIComponent("user_info projects pull_requests");
    //src="http://lightsever.gitee.io/fakenes/jmpback.html";
    //
    //  frame.src=src;
    {
        var btn = document.createElement("button");
        btn.style.width = "100px";
        btn.style.height = "24px";
        btn.style.position = "absoulte";
        btn.textContent = "Jmp Login";
        document.body.appendChild(btn);
        btn.onclick = function () {
            //src ="jmpback.html";
            jmpmode = 1;
            oauthWin = window.open(src, "_blank");
        };
    }
    {
        var btn = document.createElement("button");
        btn.style.width = "100px";
        btn.style.height = "24px";
        btn.style.position = "absoulte";
        btn.textContent = "Close Login";
        document.body.appendChild(btn);
        btn.onclick = function () {
            //src ="jmpback.html";
            jmpmode = 2;
            oauthWin = window.open(src, "_blank");
        };
    }
    {
        var btn = document.createElement("button");
        btn.style.width = "100px";
        btn.style.height = "24px";
        btn.style.position = "absoulte";
        btn.textContent = "Keep Login";
        document.body.appendChild(btn);
        btn.onclick = function () {
            //src ="jmpback.html";
            jmpmode = 0;
            oauthWin = window.open(src, "_blank");
        };
    }
}
window.onload = main;
